<?php
namespace app\index\controller;

use app\common\entity\Config;
use app\common\entity\User;
use app\common\entity\WithdrawMoney;
use app\common\shop\RechargeLogModel;
use app\common\shop\WithdrawLogModel;
use think\Db;
use think\Request;
use think\Controller;
use app\common\entity\MywalletLog;
use app\common\entity\Mywallet;

class Log extends Base {
    /**
     * [资金明细]
     * @param
     * @return
     */
    public function mylog( Request $request ){
        $uid = $this->userId;
        $page = $request->post('page',1);
        $limit = $request->post('limit',20);
        $money_type = $request->post('money_type',1);
        $types = $request->post('types');// 类型
        $where = [];
        if($types){
            $where['types'] = $types;
        }
        $list = MywalletLog::where('user_id',$uid)
            ->where('money_type',$money_type)
            ->where($where)
            ->page($page,$limit)
            ->order('id desc')
            ->select()->toArray();
        foreach ($list as $key => $value) {
            $list[$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
        }
        return json(['code' => 0, 'msg'=>'请求成功','info'=>$list]);
    }
    /**
     * [获取提现信息-余额、提现日等]
     * @param
     * @return
     */
    public function getWithdrawal( Request $request ){
        $uid = $this->userId;
        $money = (new Mywallet())->balance($uid,'money');// 余额
        $iswith = 1;
        $base_num = Config::getValue('withdraw_base');
        $withdrawal_time = Config::getValue('withdrawal_time');
        $day = date('d');
        if($withdrawal_time>31){
            $withdrawal_time = 31;
        }
        /*if($withdrawal_time != $day) {
            $iswith = 0;
        }*/
        return json(['code' => 0, 'msg'=>'请求成功','info'=>['money'=>$money,'iswith'=>$iswith,'withdrawal_time'=>$withdrawal_time,'base_num'=>$base_num,'withdrawal_fee'=>Config::getValue('withdrawal_fee')]]);
    }
    /**
     * [获取余额]
     * @param
     * @return
     */
    public function getbalance( Request $request ){
        $uid = $this->userId;
        $field = $request->post('field');
        $money = (new Mywallet())->balance($uid,$field);// 余额
        return json(['code' => 0, 'msg'=>'请求成功','info'=>['money'=>$money]]);
    }
    /**
     * [提现]
     * @param
     * @return
     */
    public function withdrawal( Request $request ){
        $uid = $this->userId;
        $user = User::where('id',$uid)->find();
        $withdrawal_time = Config::getValue('withdrawal_time');
        $day = date('d');
        if($withdrawal_time>31){
            $withdrawal_time = 31;
        }

        //if($withdrawal_time != $day) return json(['code' => 1, 'msg' => '今天不是提现日']);
        $number = $request->post('number');
        if($number <=0) return json(['code' => 1, 'msg' => '提现数量不能为空']);
        $base_num = Config::getValue('withdraw_base');
        if($base_num > 0){
            if($number%$base_num !=0 ) return json(['code' => 1, 'msg' => '提现金额必须是'.$base_num.'的倍数']);
        }

        /*$withdrawal_min = Config::getValue('withdrawal_min');
        if($number < $withdrawal_min){
            return json(['code' => 1, 'msg' => '最小提现金额为'.$withdrawal_min]);
        }*/


        $money = (new Mywallet())->balance($uid,'money');// 余额
        if($money< $number) return json(['code' => 1, 'msg' => '余额不足']);
        $fee = $number * Config::getValue('withdrawal_fee')/100;// 手续费

        $type = $request->post('type');// 提现到 支付宝 微信
        if(empty($type)) return json(['code' => 0, 'msg' => '请选择提现方式']);
        if($type == 1 && empty($user['zfb'])){
            return json(['code' => 1, 'msg' => '请先绑定支付宝']);
        }
        if($type == 2 && empty($user['wx'])){
            return json(['code' => 1, 'msg' => '请先绑定微信']);
        }
        WithdrawLogModel::add(['uid'=>$uid,'money'=>$number,'fee'=>$fee,'type'=>$type]);
        MywalletLog::addLog($uid, $number, 4, '提现', '6', 2);
        return json(['code' => 0, 'msg' => '请求成功']);
    }

    /**
     * [兑换福币]
     * @param
     * @return
     */
    public function exchange( Request $request ){
        $uid = $this->userId;
        $number = $request->post('number');// 兑换数量
        $base_num =  Config::getValue('exchange_base');
        if($base_num > 0){
            if((int)$number%$base_num !=0) return json(['code' => 1, 'msg' => '兑换金额必须是'.$base_num.'的倍数']);
        }

        $money = (new Mywallet())->balance($uid,'bless');// 余额
        if($money< $number) return json(['code' => 1, 'msg' => '福币余额不足']);
        // 扣福币
        MywalletLog::addLog($uid, $number, 1, '兑换', '8', 2);
        //增加余额
        MywalletLog::addLog($uid, $number, 4, '兑换', '8', 1);
        return json(['code' => 0, 'msg' => '兑换成功']);
    }

    /**
     * [充值记录]
     * @param
     * @return
     */
    public function rechargelog( Request $request ){
        $uid = $this->userId;
        $page = input('page');
        $limit = input('limit');
        $log = RechargeLogModel::field('*')
            ->where('uid',$uid)
            ->order('id desc')
            ->page($page,$limit)
            ->select();

        foreach ($log as $key => $value) {
            $log[$key]['createtime'] = date('Y-m-d H:i',$value['createtime']);
        }
        return json(['code' => 0, 'msg'=>'请求成功','info'=>$log]);
    }

    /**
     * [提现记录]
     * @param
     * @return
     */
    public function withdrawallog( Request $request ){
        $uid = $this->userId;
        $page = input('page');
        $limit = input('limit');
        $log = WithdrawLogModel::field('*')
            ->where('uid',$uid)
            ->order('id desc')
            ->page($page,$limit)
            ->select();
        foreach ($log as $key => $value) {
            $log[$key]['createtime'] = date('Y-m-d H:i',$value['createtime']);
            $log[$key]['finishtime'] = date('Y-m-d H:i',$value['finishtime']);
        }
        return json(['code' => 0, 'msg'=>'请求成功','info'=>$log]);
    }
}	